package lucid.hrms.management.nationality;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.binding.message.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.webflow.action.MultiAction;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;

@Component("nationalityAction")
public class NationalityAction extends MultiAction {
	private static final Logger logger = LoggerFactory
			.getLogger(NationalityAction.class);

	@Autowired
	private NationalityService nationalityService;

	public Event saveNationality(RequestContext context) {
		logger.debug("saveNationality");
		try {
			Nationality nationality = (Nationality) context.getViewScope().get(
					"nationality");
			nationality = nationalityService.saveNationality(nationality);
			context.getViewScope().put("nationality", nationality);
			context.getMessageContext().addMessage(
					new MessageBuilder().info().defaultText(
							"Nationality has been saved successfully").build());

			return success();
		} catch (Exception e) {
			logger.debug("Exception: " + e);
			context.getMessageContext().addMessage(
					new MessageBuilder().error().defaultText(
							"Failed to save Nationality record. Reason: "
									+ e.getMessage()).build());
			return error();
		}
	}
}
